package com.netty.helper;

import com.alibaba.fastjson.JSON;
import com.netty.constant.Constants;
import com.netty.model.Message;
import com.netty.model.MessageBody;
import com.netty.util.SystemInfoUtil;
import org.apache.commons.lang.time.DateFormatUtils;

import java.util.Date;

/**
 * 发送的数据创建工厂
 *
 * @author suzhen
 * @create 2018/7/13
 */
public class MessageFactory {

    /**
     * 创建一个数据载体
     *
     * @param session
     * @return
     */
    public static Message createMessage(String session) {

        SystemInfoUtil systemInfoUtil = SystemInfoUtil.getInstance();
        Message message = new Message();
        message.setVersion("1");
        message.setDeviceId(systemInfoUtil.getMac());
        message.setCmd(Constants.CmdType.BIND);
        message.setSender(session);
        message.setReceiver(session);
        message.setType(Constants.ProtobufType.SEND);
        message.setFlag(1);
        message.setPlatForm(systemInfoUtil.getSystem());
        message.setPlatFormVersion(systemInfoUtil.getSystemName());
        message.setToken(session);
        message.setAppKey("sniper");
        message.setTimeStamp(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        message.setSign("sniper");
        return message;
    }

    public static MessageBody createMessageBody(Message message) {
        return JSON.parseObject(message.getContent(), MessageBody.class);
    }
}
